package com.gopay.bis.manager.bank.ebank;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.tbank.ebank.pay.TBankMessage;
import com.gopay.common.domain.bank.tbank.ebank.query.req.TBankOrderReq;
import com.gopay.common.domain.bank.tbank.ebank.query.res.TBankOrderRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 测试银行B2B对象封装
 * 
 * @TBankB2bManager.java
 * @author WangHongFei
 */
public class TBankB2bManager implements BankManagerRemoteService {
	
	@Resource(name = "tbankConfig")
	private Map<String, String> tbankConfig;
	
	@Resource(name = "tbankB2bBankServiceClient")
	private BankService tbankB2bBankServiceClient;

	/**
	 * 测试银行签名
	 * 
     * @param BaseMessage
     * @return Object
     * @throws GopayException
	 */
	public Object sign(BaseMessage message) throws GopayException {
		TBankMessage messageB2B = (TBankMessage) message;
		messageB2B.setMsgType(tbankConfig.get("TBANK.B2B.MSGTYPE"));
		messageB2B.setTxnType(tbankConfig.get("TBANK.B2B.TXNTYPE"));
		messageB2B.setMercNo(tbankConfig.get("TBANK.B2B.MerchantNo"));
		messageB2B.setOrderUrl(tbankConfig.get("TBANK.B2B.OrderUrl"));
		messageB2B.setOrderSerUrl(tbankConfig.get("TBANK.B2B.OrderSerUrl"));
		messageB2B.setBankUrl(tbankConfig.get("TBANK.B2B.BankUrl"));
		messageB2B.setCurCode(tbankConfig.get("TBANK.CUR.CODE"));
		messageB2B.setOrderDate("20121127");
		messageB2B.setOrderTime("020610");
		messageB2B.setPlainMessage(buildMessage(messageB2B));
		messageB2B.setSignMessage(messageB2B.getPlainMessage());
		BankLogUtils.printB2BManagerSignSendLog(messageB2B);
		String signStr = (String) tbankB2bBankServiceClient.sign(messageB2B);
		messageB2B.setSignMessage(signStr);
		BankLogUtils.printB2BManagerSignReturnLog(messageB2B);
		return messageB2B;
	}

	/**
	 * 测试银行验签
	 * 
     * @param BaseMessage
     * @return Object
     * @throws GopayException
	 */
	public Object verify(BaseMessage message) throws GopayException {
		TBankMessage messageB2B = (TBankMessage) message;
		messageB2B.setMsgType(tbankConfig.get("TBANK.B2B.MSGTYPE"));
		messageB2B.setTxnType(tbankConfig.get("TBANK.B2B.TXNTYPE"));
		messageB2B.setMercNo(tbankConfig.get("TBANK.B2B.MerchantNo"));
		messageB2B.setOrderUrl(tbankConfig.get("TBANK.B2B.OrderUrl"));
		messageB2B.setOrderSerUrl(tbankConfig.get("TBANK.B2B.OrderSerUrl"));
		messageB2B.setBankUrl(tbankConfig.get("TBANK.B2B.BankUrl"));
		messageB2B.setCurCode(tbankConfig.get("TBANK.CUR.CODE"));
		messageB2B.setOrderDate("20121127");
		messageB2B.setOrderTime("020610");
		messageB2B.setPlainMessage(buildMessage(messageB2B));
		messageB2B.setSignMessage(messageB2B.getPlainMessage());
		BankLogUtils.printB2BManagerVerifySendLog(messageB2B);
		boolean flag = (Boolean) tbankB2bBankServiceClient.verify(messageB2B);
		BankLogUtils.printB2BManagerVerifyReturnLog(messageB2B, flag);
		return flag;
	}

	/**
	 * 订单查询
	 * 
     * @param Object
     * @return Object
     * @throws GopayException
	 */
	public Object query(Object message) throws GopayException {
		TBankOrderReq messageB2B = (TBankOrderReq) message;
		messageB2B.setOrderNo(messageB2B.getOrderId());
		messageB2B.setMercNo(tbankConfig.get("TBANK.B2B.MerchantNo"));
		messageB2B.setMsgType(tbankConfig.get("TBANK.B2B.QUERY.MSGTYPE"));
		messageB2B.setTxnType(tbankConfig.get("TBANK.B2B.TXNTYPE"));
		messageB2B.setBankUrl(tbankConfig.get("TBANK.B2B.BankUrl"));
		messageB2B.setSignDate(this.getCurrDate());
		BankLogUtils.printB2BManagerQuerySendLog(messageB2B, "");
		TBankOrderRes resMessage = (TBankOrderRes) tbankB2bBankServiceClient.query(messageB2B);
		resMessage.setOutStlmCd(BankConstants.BANK_LOGO_TBANK); // 为了区分日志输出
		BankLogUtils.printB2BManagerQueryReturnLog(resMessage);
		return resMessage;
	}

	/**
	 * 构建明文信息
	 * 
	 * @param TBankMessage
	 * @return String
	 */
	private String buildMessage(TBankMessage messageB2C) {
		StringBuffer sb = new StringBuffer(512);
		sb.append(StringUtils.trimToEmpty(messageB2C.getOrderId())).append("|");
		sb.append(StringUtils.trimToEmpty(messageB2C.getOrderTime())).append("|");
		sb.append(StringUtils.trimToEmpty(messageB2C.getCurCode())).append("|");
		sb.append(StringUtils.trimToEmpty(messageB2C.getAmount().toString())).append("|");
		sb.append(StringUtils.trimToEmpty(messageB2C.getMercNo()));
		return sb.toString();
	}

	/**
	 * 获取当前时间
	 * 
	 * @return String
	 */
	private String getCurrDate() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = sdf.format(date);
		return s;

	}

	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
